package com.anlogic.sdk.efuse.ui;

import java.io.File;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.anlogic.sdk.efuse.model.GroupId;
import com.anlogic.sdk.efuse.model.ProgramEfuseModel;

public class ProgramEfuseDialog extends TitleAreaDialog {
	ProgramEfuseComposite firstGroup;

	Button enableSecondGroup;
	
	ProgramEfuseComposite secondGroup;
	
	Button trngWrite;
	
	Button verifyButton;

	ProgramEfuseModel model;

	public ProgramEfuseDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		model = new ProgramEfuseModel();
	}

	protected Control createButtonBar(Composite parent) {
		Control c = super.createButtonBar(parent);
		renameOKButton();
		return c;
	}

	private void renameOKButton() {
		Button okButton = getButton(0);
		if (okButton != null) {
			okButton.setText("Program");
			setButtonLayoutData(okButton);
		}
	}

	private void enableOkButton(boolean flag) {
		Button button = getButton(0);
		if (button != null)
			button.setEnabled(flag);
	}

	protected Control createDialogArea(Composite parent) {
		setTitle("Program Efuse");
		setMessage("Program Efuse.");

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		firstGroup = new ProgramEfuseComposite(container, model, GroupId.FIRST);

		enableSecondGroup = new Button(container, SWT.CHECK);
		enableSecondGroup.setText("Enable Second Group");

		secondGroup = new ProgramEfuseComposite(container, model, GroupId.SECOND);
		secondGroup.setEnable(false);
		
		Group otherGroup = new Group(container, SWT.NONE);
		otherGroup.setText("Others");
		GridLayout layout = new GridLayout(3, false);
		otherGroup.setLayout(layout);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
		otherGroup.setLayoutData(gd);
		otherGroup.setForeground(new Color(null, 0, 0, 255));
		
		trngWrite = new Button(otherGroup, SWT.CHECK);
		trngWrite.setText("TRNG Write");
		
		verifyButton = new Button(container, SWT.CHECK);
		verifyButton.setText("Verify after program");

		addListeners();

		this.firstGroup.setUiValidator(new IUIValidator() {
			public void validate() {
				ProgramEfuseDialog.this.validateModel();
			}
		});

		this.secondGroup.setUiValidator(new IUIValidator() {
			public void validate() {
				ProgramEfuseDialog.this.validateModel();
			}
		});

		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				ProgramEfuseDialog.this.validateModel();
			}
		});

		return (Control) parent;
	}

	private void addListeners() {
		enableSecondGroup.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				secondGroup.setEnable(enableSecondGroup.getSelection());
				model.setEnableSecondGroup(enableSecondGroup.getSelection());
			}
		});
		
		trngWrite.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				model.setTRNGWrite(trngWrite.getSelection());
			}
		});
		
		verifyButton.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				model.setVerify(verifyButton.getSelection());
			}
		});
	}

	private void validateModel() {
		setErrorMessage(null);
		enableOkButton(false);

		String encKey0 = this.model.getEncryptyKey(GroupId.FIRST);
		String encKey1 = this.model.getEncryptyKey(GroupId.SECOND);
		String pubKey0 = this.model.getPpk(GroupId.FIRST);
		String pubKey1 = this.model.getPpk(GroupId.SECOND);
		
		checkFileExists(encKey0, "First Encrypty Key");
		if (getErrorMessage() != null)
			return;

		checkFileExists(pubKey0, "First PPK");
		if (getErrorMessage() != null)
			return;
		
		if (enableSecondGroup.getSelection()) {
			checkFileExists(encKey1, "Second Encrypty Key");
			if (getErrorMessage() != null)
				return;
			
			checkFileExists(pubKey1, "Second PPK");
			if (getErrorMessage() != null)
				return;
		}
		
		if (getErrorMessage() == null)
			enableOkButton(true);
	}
	
	private boolean checkFileExists(String filePath, String name) {
		if (filePath == null || filePath.isEmpty()) {
			//setErrorMessage(String.format("%s file is empty", name));
			return false;
		}
		File file = new File(filePath);
		if (!file.exists()) {
			setErrorMessage(String.format("%s does not exits", name));
			return false;
		} else if (!file.isFile()) {
			setErrorMessage(String.format("%s is a directory", name));
			return false;
		}
		return true;
	}

	protected void okPressed() {
		super.okPressed();
	}
	
	public ProgramEfuseModel getModel() {
		return model;
	}
}
